{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 构建数据\r\n",
    "\r\n",
    "```py\r\n",
    "import random\r\n",
    "f = open('data.txt', 'w')\r\n",
    "stu = ['Bernard', 'Kluicer', 'Anton', 'Falcon', 'Zima']\r\n",
    "\r\n",
    "\r\n",
    "def randTime():\r\n",
    "    t = str(random.randint(6, 19)).zfill(2)\r\n",
    "    m = str(random.randint(0, 59)).zfill(2)\r\n",
    "    s = str(random.randint(0, 59)).zfill(2)\r\n",
    "    return t+':'+m+':'+s\r\n",
    "\r\n",
    "\r\n",
    "print('ID\\tname\\tamount\\tpos\\tdate\\ttime', file=f)\r\n",
    "\r\n",
    "ordId = 0\r\n",
    "\r\n",
    "for day in range(1, 6):\r\n",
    "    for _ in range(random.randint(5*2, 5*3+3)):\r\n",
    "        ordId += 1\r\n",
    "        name = random.choice(stu)\r\n",
    "        amount = random.randint(60, 200)/10\r\n",
    "        pos = random.randint(1, 6)\r\n",
    "        tm = randTime()\r\n",
    "        print(str(ordId)+'\\t'+name+'\\t'+str(amount)+'\\t' +\r\n",
    "              str(pos)+'\\t'+'Sept '+str(day)+'\\t'+tm, file=f)\r\n",
    "```\r\n",
    "\r\n",
    "![数据](https://img-blog.csdnimg.cn/img_convert/64d7bf01b126024bda8a0554b2b9479e.png)"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 载入数据\r\n",
    "\r\n",
    "顺便看看数据的基本信息"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import pandas as pd\r\n",
    "df = pd.read_table('data.txt')\r\n",
    "df"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.info()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.describe()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 分析数据\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 不吃早餐"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.query('time<\"10:00:00\"').groupby('name')['amount'].describe().sort_values"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "设定十点前的才算早餐，可以看出来Kluicer经常不吃早餐。  \r\n",
    "当然这张表也可以看出来他们早餐花费的一些数据特征。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 热点餐位"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.groupby('pos').describe()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.groupby('pos')['amount'].describe().sort_values('count', ascending=False)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "6号餐位最受欢迎，产生了最多的订单"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 贫困学生"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "统计各位同学总消费金额并排序"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.groupby('name')['amount'].sum().sort_values()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.groupby('name')['amount'].describe().sort_values('count')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看出来Anton和Bernard比较贫困"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 丰盛晚餐"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "df.query('time>\"16:00:00\"').groupby('name')['amount'].describe().sort_values\r\n",
    "# 可以看出来Bernard最舍得在晚餐上花钱"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.5"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.5 64-bit"
  },
  "interpreter": {
   "hash": "a9661a0f83c933cf25e0dd40af67627cb20747a37143c65b775945a664681eb4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}